package com.phone.download;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.cloudtech.multidownload.MultiDownloadManager;
import com.cloudtech.multidownload.entitis.FileInfo;

public class FileAdapter extends BaseAdapter {
	
	private Context mContext = null;
	private List<FileInfo> mFilelist = null;
	
	private LayoutInflater layoutInflater;
	private static ViewHolder viewHolder = null;

	public FileAdapter(Context mContext, List<FileInfo> mFilelist) {
		super();
		this.mContext = mContext;
		this.mFilelist = mFilelist;
		layoutInflater = LayoutInflater.from(mContext);
	}

	@Override
	public int getCount() {
		return mFilelist.size();
	}

	@Override
	public Object getItem(int position) {
		return mFilelist.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final FileInfo mFileInfo = mFilelist.get(position);
		if(convertView == null) {
			convertView = layoutInflater.inflate(R.layout.item, null);
			viewHolder = new ViewHolder();
			viewHolder.textview = (TextView) convertView.findViewById(R.id.file_textview);
			viewHolder.startButton = (Button) convertView.findViewById(R.id.start_button);
			viewHolder.stopButton = (Button) convertView.findViewById(R.id.stop_button);
			viewHolder.progressBar = (ProgressBar) convertView.findViewById(R.id.progressBar2);
			
			
			viewHolder.textview.setText(mFileInfo.getFileName());
			viewHolder.progressBar.setMax(100);
			
			viewHolder.startButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Log.e("jw", "onClick: startButton");
					MultiDownloadManager.startDownloadFile(mContext, mFileInfo);
					viewHolder.startButton.setClickable(false);
					viewHolder.stopButton.setClickable(true);

				}
			});
			
			viewHolder.stopButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Log.e("jw", "onClick: stopButton");
					MultiDownloadManager.stopDownloadFile(mContext, mFileInfo);
					viewHolder.startButton.setClickable(true);
					viewHolder.stopButton.setClickable(false);
				}
			});

			Log.e("jw", "getView: error ->" + mFileInfo.isError());
			viewHolder.startButton.setClickable(mFileInfo.isError());
			viewHolder.stopButton.setClickable(!mFileInfo.isError());
			
			convertView.setTag(viewHolder);
		} else{
			viewHolder = (ViewHolder) convertView.getTag();
		}
		
		viewHolder.progressBar.setProgress((int) mFileInfo.getFinished());
		
		return convertView;
	}
	
	public void updataProgress(int id, int progress) {
		FileInfo info = mFilelist.get(id);
		info.setFinished(progress);
		notifyDataSetChanged();
	}
	
	public void updateError(int id){
		notifyDataSetChanged();
	}
	
	static class ViewHolder{
		 TextView textview;
		 Button startButton;
		 Button stopButton;
		 ProgressBar progressBar;
	}

}
